package com.ksd.api.controller;

import com.ksd.api.beans.*;
import com.ksd.api.tool.VodDataRecommend;
import com.ksd.api.utils.HttpServletUtil;
import com.ksd.common.core.controller.BaseController;
import com.ksd.common.core.domain.AjaxResult;
import com.ksd.common.utils.DateUtils;
import com.ksd.common.utils.StringUtils;
import com.ksd.system.domain.*;
import com.ksd.system.service.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

@RestController
public class GetSongMedia extends BaseController {
    private static final Logger logger = LoggerFactory.getLogger(GetSongMedia.class);
    @Autowired
    private ISysMusicAlbumService sysMusicAlbumService;
    @Autowired
    private ISysMusicDataService sysMusicDataService;
    @Autowired
    private ISysBoardService sysBoardService;


    @RequestMapping(value = "/api/user/vod/getSongMedia", method = RequestMethod.GET)
    @ResponseBody
    public AjaxResult getTvType(HttpServletRequest httpServletRequest) {
        logger.info("===================指定专辑分类,分页获取该专辑下的所有歌曲列表===============");
        HttpServletUtil.getRequestParam(httpServletRequest);

        String catId = httpServletRequest.getParameter("cat_id");
        String keyword = httpServletRequest.getParameter("keyword");


        SysBoard sysBoard = sysBoardService.selectSysBoardById(SysBoard.serviceFileId);
        String serviceIpUrl = sysBoard.getUrl();
        SongMediaResponse songMediaResponse = new SongMediaResponse();
        SysMusicAlbum sysMusicAlbum = sysMusicAlbumService.selectSysMusicAlbumById(Long.valueOf(catId));
        SongMediaObject songMediaObject = new SongMediaObject();
        if (StringUtils.isNotNull(sysMusicAlbum)){
            SysMusicData sysMusicDataRe = new SysMusicData();
            sysMusicDataRe.setAlbumId(sysMusicAlbum.getId());
            List<SysMusicData> sysMusicDataList = sysMusicDataService.selectSysMusicDataList(sysMusicDataRe);
            songMediaObject.setAutoid(sysMusicAlbum.getId());
            songMediaObject.setTitle(sysMusicAlbum.getTitle());
            songMediaObject.setView_count(sysMusicDataList.size());
            songMediaObject.setImgAddress(serviceIpUrl+sysMusicAlbum.getImgUrl());
            songMediaObject.setType("vod");
        }

        SysMusicData sysMusicData = new SysMusicData();
        if (StringUtils.isNotEmpty(catId)) {
            sysMusicData.setAlbumId(Long.valueOf(catId));
        }
        if (StringUtils.isNotEmpty(keyword)) {
            sysMusicData.setKeyWord(keyword);
        }
        startPage();
        List<SysMusicData> sysMusicDataList = sysMusicDataService.selectSysMusicDataList(sysMusicData);
        List<SongAddress> songAddressList = new ArrayList<>();
        for (SysMusicData result : sysMusicDataList) {
            SongAddress songAddress = new SongAddress();
            songAddress.setId(result.getId());
            songAddress.setVod_id(sysMusicAlbum.getId());
            songAddress.setNum(1);
            songAddress.setName(result.getTitle());
            songAddress.setUrl_address(serviceIpUrl+result.getMusicUrl());
            songAddress.setUrlAddress(serviceIpUrl+result.getMusicUrl());
            songAddress.setSinger(result.getSinger());
            songAddress.setImg_address(serviceIpUrl+result.getImgUrl());
            songAddress.setImage_address(serviceIpUrl+result.getImgUrl());
            songAddress.setImgAddress(serviceIpUrl+result.getImgUrl());
            songAddress.setImageAddress(serviceIpUrl+result.getImgUrl());
            songAddress.setStatus(0);
            songAddress.setCreated_at(DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS,result.getCreateTime()));
            songAddress.setTitle(result.getTitle());
            songAddress.setFiles_type("music");
            songAddress.setType("group");
            songAddress.setSong_word(result.getSongWord());
            songAddressList.add(songAddress);
        }
        songMediaObject.setGroup_url_address(songAddressList);
        songMediaResponse.setVideo(songMediaObject);

        return AjaxResult.success(songMediaResponse);
    }
}